﻿@{
    ViewData["Title"] = "ShowEntityMarket";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1 style="text-align:center;padding-bottom:20px">供应商列表</h1>

<div>
    <table>
        <tbody>
        <td style="padding-bottom:20px">
            <div class="layui-form layui-row">
                <label class="layui-form-label" style="width:100px">供应商编号：</label>
                <div class="layui-col-md3" style="width:200px">
                    <input type="text" name="SupplierCode" id="SupplierCode" placeholder="请输入" class="layui-input">
                </div>
            </div>
        </td>
        <td style="padding-bottom:20px">
            <div class="layui-form layui-row">
                <label class="layui-form-label" style="width:100px">供应商名称：</label>
                <div class="layui-col-md3" style="width:200px">
                    <input type="text" name="SupplierName" id="SupplierName" placeholder="请输入" class="layui-input">
                </div>
            </div>
        </td>
        <td style="padding-bottom:20px">
            <div class="layui-form layui-row">
                <label class="layui-form-label" style="width:100px">负责人电话：</label>
                <div class="layui-col-md3" style="width:200px">
                    <input type="text" name="PrincipalTel" id="PrincipalTel" placeholder="请输入" class="layui-input">
                </div>
            </div>
        </td>
        
        <td style="padding-bottom:20px">
            <div class="layui-form layui-row">
                <label class="layui-form-label" style="width:100px">服务人员：</label>
                <div class="layui-col-md3" style="width:200px">
                    <input type="text" name="SupplierService" id="SupplierService" placeholder="请输入" class="layui-input">
                </div>
            </div>
        </td>
        </tr>

        <tr>
            <td style="padding-bottom:20px">
                <div class="layui-form layui-row">
                    <label class="layui-form-label" style="width:100px">使用状态：</label>


                    <div class="layui-input-inline layui-input-wrap">
                        <select id="SupplierState" name="SupplierState">
                            <option value="">请选择</option>
                            <option value="1">启用</option>
                            <option value="2">停用</option>
                        </select>
                    </div>
                </div>

            </td>
            <td style="padding-bottom:20px">
                <div class="layui-inline">
                    <button type="button" class="layui-btn layui-btn-primary" id="searchBtn" style="background-color:red;color:aliceblue"><i class="layui-icon"></i> 搜 索</button>
                    <button type="button" class="layui-btn layui-bg-red" onclick="location.href='/Supplier/AddSupplier'"><i class="layui-icon layui-icon-addition">新增</i></button>
                    <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn"><i class="layui-icon layui-icon-delete">删除</i></a>
                </div>
            </td>
        </tr>
        </tbody>
    </table>

    <table id="ID-table-demo-setRowChecked"></table>
    <div id="pagesbox"></div>
</div>

<script>
    layui.use(['table', 'laypage'], function () {
        var table = layui.table;
        var laypage = layui.laypage;
        var $ = layui.$;
        var laydate = layui.laydate;
        var layer = layui.layer; // 添加layui弹出层模块

        // 日期范围 - 左右面板联动选择模式
        laydate.render({
            elem: '#ID-laydate-rangeLinked',
            range: ['#ID-laydate-start-date-1', '#ID-laydate-end-date-1'],
            rangeLinked: true // 开启日期范围选择时的区间联动标注模式 ---  2.8+ 新增
        });

        var size = 15; // 每页显示条数
        //获取列表
        function getListData(page) {
            //获取查询条件
            $.ajax({
                url: 'http://10.31.60.18:8000/AuthorizationPeopleGetRout/api/Supplier/ShowSupplier',
                type: 'get',
                dataType: 'json',
                //参数
                data: {
                    SupplierCode: $('#SupplierCode').val(),
                    SupplierName: $('#SupplierName').val(),
                    PrincipalTel: $('#PrincipalTel').val(),
                    SupplierService: $('#SupplierService').val(),
                    SupplierState: $('#SupplierState').val(),
                    pageIndex: page,
                    pageSize: size,
                },
                success: function (res) {
                    //渲染表格数据
                    table.render({
                        //挂载的表格id
                        elem: '#ID-table-demo-setRowChecked',
                        cols: [[
                            { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                            { type: 'numbers', title: '序号', width: 60, fixed: 'center', unresize: true, align: 'center' },
                            { field: 'supplierCode', title: '供应商编号', width: 120, align: 'center' },
                            { 
                                field: 'supplierType', title: '供应商类型', width: 120, align: 'center',
                                templet:function(data){
                                    if (data.supplierType==1) return '个人'
                                    if (data.supplierType==0) return '企业'
                                }
                            },
                            { field: 'principalTel', title: '负责人电话', width: 120, align: 'center' },
                            { field: 'principalName', title: '负责人姓名', width: 120, align: 'center' },
                            { field: 'supplierName', title: '供应商名称', width: 120, align: 'center' },
                            { field: 'principalCard', title: '身份证号码', width: 120, align: 'center' },
                            { field: 'principalocation', title: '所在地', width: 120, align: 'center' },
                            { field: 'supplierSocial', title: '社会信用代码', width: 120, align: 'center' },
                            { field: 'inputIdName', title: '录入单位', width: 120, align: 'center' },
                            { field: 'buildingIdName', title: '使用单位', width: 120, align: 'center' },


                            { 
                                field: 'supplierState', title: '使用状态', width: 120, align: 'center',
                                templet: function (data) {
                                    if (data.supplierState == 1) return '启用'
                                    if (data.supplierState == 2) return '停用'
                                }
                            },



                            { field: 'supplierService', title: '服务人员', width: 120, align: 'center' },

                            { title: '操作', width: 200, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                            
                        ]],
                        toolbar: '#demo-toolbar-setRowChecked',
                        initSort: { // 设置初始排序
                            field: 'experience', // 字段名
                            type: 'asc' // 倒序
                        },
                        data: res.data
                    });
                    //分页
                    laypage.render({
                        //挂载的分页id
                        elem: 'pagesbox',
                        count: res.tatalCount,
                        limit: size,
                        curr: page,
                        layout: ['count', 'prev', 'page', 'next', 'skip'],
                        jump: function (obj, first) {
                            if (!first) {
                                getListData(obj.curr);
                            }
                        }
                    });
                }
            });
        }
        $('#searchBtn').on('click', function () {
            getListData(1);
        });
        // 单删除
        function deleteData(marketId) {
            $.ajax({
                url: 'http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/EntityMarket/FalseDeleteEntityMarket?MarketId=' + marketId,
                type: 'delete',
                success: function (res) {
                    if (res > 0) {
                        layer.msg('删除成功');
                        getListData(1);
                    } else {
                        layer.msg('删除成功');
                        getListData(1);
                    }
                },
                error: function () {
                    layer.msg('删除请求失败');
                }
            });
        }
        // 编辑删除按钮点击事件
        table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
            var data = obj.data; // 获取当前行数据
            var supplierId = data.supplierId; // 获取当前行数据的唯一标识符
            if (obj.event === 'edit') {
                var url = `/Supplier/UpdAddSupp?supplierId=${supplierId}`;
                location.href = url; // 点击编辑按钮时跳转到修改页面
            }
            if (obj.event == 'delete') {
                layer.confirm('确定删除吗？', function (index) {
                    //(data.supplierId); // 传递正确的数据项ID
                    layer.close(index);
                });
            }
        });

        // 批量删除
        $(".delAll_btn").click(function () {
            var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
                data = checkStatus.data,
                marketId = "";
            if (data.length > 0) {
                for (var i in data) {
                    marketId += data[i].marketId + ",";
                }
                marketId = marketId.substring(0, marketId.length - 1);
                console.log(marketId);
                layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                    $.ajax({
                        url: 'http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/EntityMarket/AllFalseDeleteEntityMarket?MarketId=' + marketId,
                        type: 'put',
                        success: function (res) {
                            if (res > 0) {
                                layer.msg("批量删除成功");
                                getListData(1);
                            } else {
                                layer.msg("批量删除失败");
                            }
                        },
                        error: function () {
                            layer.msg('批量删除请求失败');
                        }
                    });
                    layer.close(index);
                });
            } else {
                layer.msg("请选择需要删除的记录");
            }
            console.log(checkStatus);
        });

        //绑定部门下拉框
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/Cottage/DeparList',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    data = res; // 直接使用返回的数据
                    var select = $('#DepartmentId');
                    $.each(data, function (index, item) {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.id).text(item.name);
                        select.append(option);
                    });
                    // 手动触发 layui 组件的渲染
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });

        //绑定耳号
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesAllInfo',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    var enumData = res; // 直接使用返回的数据
                    var select = $('#earNnumber');
                    $.each(enumData, function (index, item) {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.earNnumber).text(item.earNnumber);
                        select.append(option);
                    });
                    // 手动触发 layui 组件的渲染
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });

        // 初始化加载数据
        $(function () {
            getListData(1);
        })
    });

</script>

<script type="text/html" id="barDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-xs" lay-event="edit"> 修改 </a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete"> 删除 </a>
    </div>
</script>